home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 August / August CD.bin / Shareware / Education / numericalmethods Folder / chap_7 / adapt.m < prev    next >
Encoding:
Text File  |  1994-06-05  |  1.3 KB  |  56 lines  |  [MATF/MATL]

  1. function [SRmat,quad,err] = adapt(f,a,b,tol)
  2. % [SRmat,quad,err] = adapt(f,a,b,tol)
  3. % Adaptive Quadrature using Simpson`s rule.
  4. % f is the name of the function, input.
  5. % a is the left  endpoint, input.
  6. % b is the right endpoint, input.
  7. % toler is the tolerance, input.
  8. % SRmat is the table of values, output.
  9. % quad is the quadrature value, output.
  10. % err is the error estimate, output.
  11. SRmat = zeros(30,6);
  12. iterating = 0;
  13. done = 1;
  14. SRvec = zeros(1:6);
  15. SRvec = srule('f',a,b,tol);
  16. SRmat(1,1:6) = SRvec;
  17. m = 1;
  18. state = iterating;
  19. while (state == iterating)
  20.   n = m;
  21.   for j=n:-1:1,
  22.     p = j;
  23.     SR0vec = SRmat(p,:);
  24.     err = SR0vec(5);
  25.     tol = SR0vec(6);
  26.     if  (tol <= err),
  27.       state = done;
  28.       SR1vec = SR0vec;
  29.       SR2vec = SR0vec;
  30.       a = SR0vec(1);
  31.       b = SR0vec(2);
  32.       c = (a + b)/2;
  33.       err = SR0vec(5);
  34.       tol = SR0vec(6);
  35.       tol2 = tol/2;
  36.       SR1vec = srule('f',a,c,tol2);
  37.       SR2vec = srule('f',c,b,tol2);
  38.       err = abs(SR0vec(3)-SR1vec(3)-SR2vec(3))/10;
  39.       if  (err < tol),
  40.         SRmat(p,:) = SR0vec;
  41.         SRmat(p,4) = SR1vec(3) + SR2vec(3);
  42.         SRmat(p,5) = err;
  43.       else
  44.         SRmat(p+1:m+1,:) = SRmat(p:m,:);
  45.         m = m+1;
  46.         SRmat(p,:) = SR1vec;
  47.         SRmat(p+1,:) = SR2vec;
  48.         state = iterating;
  49.       end
  50.     end
  51.   end
  52. end
  53. quad = sum(SRmat(:,4));
  54. err = sum(abs(SRmat(:,5)));
  55. SRmat = SRmat(1:m,1:6);
  56.